lib.use(['element', 'table', 'form','tablePlug'], function () {
    var table = layui.table;
    var layer = layui.layer;
    var form = layui.form;
    var tablePlug = layui.tablePlug;
    var searchFrm = layui.Form('searchFrm');
    var receiDetailTable;
    var probId=common.util.getParam("probId")
    //初始表格基础参数
    table.set(common.layui.table.config(tablePlug));

    searchFrm.selectRender();

    // 渲染表格
    receiDetailTable = table.render({
            elem: '#receiDetailTable'
            , url: common.util.createUrl('/fin/receiDetail/list')
            ,where:{probId:probId}
            ,toolbar: '#toolbar'
            ,cols: [[
                {type:'checkbox'}
               ,{type: 'numbers', title: '编号' }
                ,{ field: 'id',  hide: true}
                ,{ title: '单据编码', field: 'receiNo' }
                ,{ title: '单据日期', field: 'receiDate' }
                ,{ field: 'storeId',  hide: true}
                ,{ title: '门店名称', field: 'storeName' }
                ,{ field: 'orderId',  hide: true}
                ,{ title: '预订单编码', field: 'probNo'}
                ,{ field: 'customerId',  hide: true}
               // ,{ title: '客户编码', field: 'customerCode' }
                //,{ title: '客户名称', field: 'customerName' }
                //,{ title: '客户联系人', field: 'customerContactName' }
                //,{ title: '客户电话', field: 'customerContactTel' }
                //,{ title: '送货地址', field: 'customerContactAdd' }
                ,{ title: '订单状态', field: 'receiState' ,templet: function (row) { return common.dist.getValue("fin.ReceiDetail_ReceiStateEnum",row.receiState);}}
                ,{ field: 'checkUserId',  hide: true}
                ,{ field: 'checkUserName',  hide: true}
                ,{ title: '审核时间', field: 'checkDate' }
                ,{ field: 'payTypeId',  hide: true}
                ,{ title: '收款类型', field: 'payTypeName' }
                ,{ field: 'payMentId',  hide: true}
                ,{ title: '收款名称', field: 'payMentName' }
                ,{ field: 'bankAccountId',  hide: true}
                ,{ title: '收款银行名称', field: 'bankAccountName' }
                ,{ field: 'remark',  hide: true}
                ,{ field: 'createUserId',  hide: true}
                ,{ field: 'createUserName',  hide: true}
                ,{ title: '创建时间',field: 'createTime'}
               /* ,{ title: '修改人id', field: 'updateUserId',  hide: true}
                ,{ title: '修改人', field: 'updateUserName' }*/
                //,{ title: '修改时间', field: 'updateTime' }
            ]]
            /*,done:function () {
            layer.closeAll('loading');
        }*/
    });

    /**
     * 监听行单击事件，进入编辑事件
     */
    table.on('rowDouble(receiDetailTableFilter)', function(obj){
        update(obj)
    });

    /**
     * 监听搜索条件提交事件
     */
    form.on('submit(searchFilter)', function(data){
        var param = data.field;
        searchTable(param)
        return false;
    });


    /**
     * 查询表格
     * @param params
     */
    function searchTable(params) {
        if(!params){
            params=common.from.serializeObject('#searchFrm');
        }
        table.reload('receiDetailTable',{where:params,page: {curr:1}});
        layer.close(layer.index);
    }

    /**
     * 查询表格
     * @param params
     */
    function add(params) {
        var config = {
            busStyle:1,
            title:'添加收款单',
            content: 'fin/receiDetail/receiDetailForm.html?probId='+probId+"&busStyle=1",
        }
        parent.common.util.openWindow(busCommon.windowConfig(config))
    }

    /**
     * 修改
     * @param obj
     */
    function update(obj) {
        var postData = [];
        if(obj.data.constructor === Array){
            postData=obj.data.map(function (item) {
                return item['id'];
            });
        }else{
            postData=[obj.data.id]
        }
        if(postData.length!=1){common.util.error('请选择一条记录');return;}
        var config = {
            busStyle:1,
            title:'修改收款单',
            content: 'fin/receiDetail/receiDetailForm.html?id=' + postData[0]+"&busStyle=1",
        }
        parent.common.util.openWindow(busCommon.windowConfig(config))
    }

    //头工具栏事件
    table.on('toolbar(receiDetailTableFilter)', function(obj){
        if(obj.event=='del'){
            del(table.checkStatus('receiDetailTable'))
        }else if(obj.event=='add'){
            add(table.checkStatus('receiDetailTable'))
        }else if(obj.event=='export'){
            var params=common.from.serializeObject('#searchFrm');
            params['pages']=receiDetailTable.config.page.pages;
            params['limit']=receiDetailTable.config.page.limit;
            params['probId']=probId;
            parent.parent.common.util.exportWindows('/fin/receiDetail/export',params)
        }else if(obj.event=='check'){
            check(table.checkStatus('receiDetailTable'))
        }else if(obj.event=='refresh'){
            searchTable();
        }

    });


    /**
     * 删除
     * @param obj
     */
    function del(obj) {
        var postData = [];
        if(obj.data.constructor === Array){
            postData=obj.data.map(function (item) {
                return item['id'];
            });
        }else{
            postData=[obj.data.id]
        }

        if(postData.length!=1){  parent.parent.common.util.error('请选择一条记录');return;}
        parent.parent.layer.confirm('确认删除!', function(index){
            console.log('Del param is', postData);
            common.ajax.post('/fin/receiDetail/delete', postData, function (resp) {
                searchTable();
                parent.parent.layer.close(index);
                parent.parent.common.util.success("删除成功")
            },function(resp){
                parent.parent.layer.close(index);
                parent.parent.common.util.error(resp.msg)
            })
        });
    }

    /**
     * 审核
     * @param obj
     */
    function check(obj) {
        if(!common.auth.hasPermission("fin:receiDetail:check")){return;}
        var postData = [];
        if(obj.data.constructor === Array){
            postData=obj.data.map(function (item) {
                return item['id'];
            });
        }else{
            postData=[obj.data.id]
        }

        if(postData.length!=1){parent.parent.common.util.error('请选择一条记录进行审核');return;}
        parent.parent.common.util.confirm('确认审核!', function(index){
            console.log('Config Check param is', postData);
            common.ajax.get('/fin/receiDetail/check/'+postData[0], null, function (resp) {
                searchTable();
                parent.parent.layer.close(index);
                parent.parent.common.util.success("审核成功")
            },function(resp){
                parent.parent.layer.close(index);
                parent.parent.common.util.error(resp.msg)
            })
        });
    }
    window.formCallBack=function () {
        searchTable();
    }
});
